package portal;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import javax.portlet.GenericPortlet;
import javax.portlet.ActionRequest;
import javax.portlet.RenderRequest;
import javax.portlet.ActionResponse;
import javax.portlet.RenderResponse;
import javax.portlet.PortletException;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.portlet.PortletRequestDispatcher;
import tavernacon.RESTClient;

/**
 * UploadWorkflow Portlet Class
 */
public class UploadWorkflow extends GenericPortlet {

    public void processAction(ActionRequest request, ActionResponse response) throws PortletException,IOException {

    }
    
    public void doView(RenderRequest request,RenderResponse response) throws PortletException,IOException {
        response.setContentType("text/html");        
        PortletRequestDispatcher dispatcher =
        getPortletContext().getRequestDispatcher("/WEB-INF/jsp/UploadWorkflow_view.jsp");
        dispatcher.include(request, response);
        String file = request.getParameter("wf");
        System.out.println("File: "+file);
    }

    /*public void uploadWorkflow() {

        try {
            RESTClient tavernaRESTClient = new RESTClient();
            tavernaRESTClient.setBaseURL("http://localhost:9090/taverna-server-0.2.1/rest");

            InputFile inputFile = (InputFile) event.getSource();
            FileInfo fileInfo = inputFile.getFileInfo();

            System.out.println("name: " + fileInfo.getFileName());
            //PushWorkflow.workflow = fileInfo.getFileName().substring(0, fileInfo.getFileName().lastIndexOf("."));

            System.out.println(inputFile.getFile());

            // Open the file that is the first
            // command line parameter
            FileInputStream fstream = new FileInputStream(inputFile.getFile());
            // Get the object of DataInputStream
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
            String workflowXml = "";
            //Read File Line By Line
            while ((strLine = br.readLine()) != null) {
                // Print the content on the console
                //System.out.println(strLine);
                workflowXml += strLine;
            }
            //Close the input stream
            in.close();
            //System.out.println(workflowXml);


            //while(!inputFile.getFileInfo().isFinished()){
            System.out.println(inputFile.getFileInfo().isFinished());
            //Thread.sleep(10000);
            //}


            //String workflowXML = IOUtils.toString(getClass().getResource("../workflows/" + fileInfo.getFileName()).openStream());
            Long workflowID = tavernaRESTClient.addWorkflow(workflowXml);
            System.out.println("WorkflowId :" + workflowID);
            workflowId.setValue("Workflow uploaded. ID: " + workflowID);

            showWorkflows();
            PushWorkflow.pushServerUpload(event, workflowID);

        } catch (Exception e) {
            System.out.println(e);
        }

    }*/
}